package com.example.fw;

import org.openqa.selenium.By;


import com.example.tests.ContactObject;

public class ContactHelper extends HelperWithWebDriverBase {

	public ContactHelper(ApplicationManager manager){
		super(manager);
	}
	
	public void createContact(ContactObject validContact) {
		manager.getNavigationHelper().openMainPage();
		manager.getNavigationHelper().goToContactPageAddNew();
		fillContactForm(validContact, true);
		submitContactCreation();
		returnToHomePage();
	}

	// Now it is like "submitGroupCreation". But if later change the title only for page Contact. 
	private void submitContactCreation() {
		click(By.name("submit"));
	}

	
	public void modifyContact (int i, ContactObject validContact){
		manager.getNavigationHelper().openMainPage();
		initContactModification(i);
	    fillContactForm(validContact, false);
	    submitContactModification();
	    returnToHomePage();
	}
	
	private void initContactModification(int i) {
		By initContactModificationButton = By.cssSelector("img[alt=\'Edit\']");
		click(initContactModificationButton);		
	}

	private void submitContactModification() {
		click(By.name("update"));
	}
	
	public void seeContactDetails (int i){
		manager.getNavigationHelper().openMainPage();
		initContactDetail(i);
	    returnToHomePageFromMenu();
	}

	private void initContactDetail(int i) {
		By initContactDetailButton = By.cssSelector("img[alt=\'Details\']");
		click(initContactDetailButton);			
	}

	public void printContactFromDetails (int i){
		manager.getNavigationHelper().openMainPage();
		initContactDetail(i);
	    goToPrintContactPage();
	}
	
	private void goToPrintContactPage() {
		click(By.name("print"));
		
	}

	private void fillContactForm(ContactObject contactObject, boolean fillGroup) {
		type(By.name("firstname"), contactObject.firstname);
		type(By.name("lastname"), contactObject.lastname);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.home);
		type(By.name("mobile"), contactObject.mobile);
		type(By.name("work"), contactObject.work);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);
		type(By.name("byear"), contactObject.byear);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.phone2);	
		select(By.name("bday"), contactObject.bday);
  		select(By.name("bmonth"), contactObject.bmonth);
  		if (fillGroup){
  	  		select(By.name("new_group"), contactObject.new_group);  			
  		}
	}

	private void returnToHomePage() {
		click(By.linkText("home page"));
	}

	private void returnToHomePageFromMenu() {
		click(By.linkText("home"));
	}
	

}
